package threads.multirun;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MultuTaskRunner {

	private static Long taskAmount = 3L;
	
	public static void main(String[] args) {
		
		List<Task> taskList = new ArrayList<Task>();
		for (long i = 0; i < taskAmount	; i++) {
			taskList.add(new Task(i+1));
		}
		
		//runConsecutively(taskList);
		try {
			runSimultaneously(taskList);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private static void runSimultaneously(List<Task> taskList) throws InterruptedException {
		
		ExecutorService executorService = Executors.newFixedThreadPool(taskList.size());
		
		executorService.invokeAll(taskList);
		
	}
	
	private static void runConsecutively(List<Task> taskList) throws Exception {
		for (Task task : taskList) {
			task.call();
		}
	}
	
}
